/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.ejb;

import entities.Fantasyrosterperweek;
import entities.Fantasyteam;
import entities.Nflplayer;
import entities.Week;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author The Lunatics
 */
@Stateless
public class FantasyrosterperweekFacade extends AbstractFacade<Fantasyrosterperweek> {

    @PersistenceContext(unitName = "Touchdown308PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FantasyrosterperweekFacade() {
        super(Fantasyrosterperweek.class);
    }

    public List<Fantasyrosterperweek> findByTeamByWeek(Fantasyteam team, Week week) {
        Query cq = getEntityManager().createQuery("SELECT f from Fantasyrosterperweek f WHERE f.teamId = :teamId AND f.weekId = :weekId");
        cq.setParameter("teamId", team);
        cq.setParameter("weekId", week);
        return cq.getResultList();
    }

     
     public void findByTeamIdByWeekIdByPlayerId(Fantasyteam teamId, Week weekId, Nflplayer playerId) 
     {
        Query createQuery = getEntityManager().createQuery("select f from Fantasyrosterperweek f where f.teamId = :teamId and f.weekId = :weekId and f.playerId = :playerId");
        
        
        
        createQuery.setParameter("teamId", teamId);
        createQuery.setParameter("weekId", weekId);
        createQuery.setParameter("playerId", playerId);

        Fantasyrosterperweek ff = (Fantasyrosterperweek)createQuery.getSingleResult(); 
        
        getEntityManager().remove(ff);
        
    }
     
     public List<Fantasyrosterperweek> findByTeamIdByWeekId(String teamId, String weekId) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Query q = em.createNamedQuery("Fantasyrosterperweek.findByDateTime");
        q.setParameter("teamId", teamId);
        q.setParameter("weekId", weekId);
        return q.getResultList();
    }
     
     
 
     public void updateRoster(List<Fantasyrosterperweek> l)
     {
         for(int i = 0; i < l.size() ; i++)
         {

            getEntityManager().merge(l.get(i));
        }
    }
}
